import { defineStore } from 'pinia'

export interface ICommonState {
  cacheAction: Map<string, any>;
  loadingPage: number;
  collapsedMenu: boolean;
}

export type IChangeLoading = 'add' | 'sub' | 'reset'

export const useCommonStore = defineStore('common', {
  state (): ICommonState {
    return {
      loadingPage: 0,
      cacheAction: new Map(),
      collapsedMenu: false
    }
  },
  actions: {
    handleSetCacheAction (key: string, data: any) {
      if (this.cacheAction.get(key)) {
        this.cacheAction.delete(key)
      }
      this.cacheAction.set(key, data)
    },
    toggleCollapsed () {
      this.collapsedMenu = !this.collapsedMenu
    },
    handleChangeLoading (type: IChangeLoading) {
      if (type === 'add') {
        this.loadingPage = this.loadingPage + 1
        return
      }
      if (type === 'sub') {
        this.loadingPage = this.loadingPage - 1
        return
      }
      this.loadingPage = 0
    }
  }
})
